<?php

require_once 'Zend/View/Helper/FormElement.php';

class Zend_View_Helper_FormDate extends Zend_View_Helper_FormElement
{

	protected static $dateTypes = array(
		'M'=>'str','j'=>'int','Y'=>'int'
	);
	
	protected static $minMax = array(
		'M'=>array(1,12),
		'j'=>array(1,31),
		'Y'=>array(1990,'defReplace')
	);
	
	protected static $defaults = array(
		'',0,0
	);
	
    public function formDate($name, $value = null, $attribs = null,
        $options = null, $listsep = "<br />\n")
    {
        $info = $this->_getInfo($name, $value, $attribs, $options, $listsep);
        extract($info); // name, id, value, attribs, options, listsep, disable

        // force $value to array so we can compare multiple values
        // to multiple options.
        
        //make sure current date has been set.
        if ( self::$minMax['Y'][1] == 'defReplace' ) {
        	self::$minMax['Y'][1] = (Int)date('Y', strtotime("+10 year",time()));
        } 
        
        if ( is_numeric($value) ) {
        	$val = array();
        	
        	foreach ( self::$dateTypes as $key=>$type) {
        		
        		switch( $type ) {
        			case 'str':
        				$val[] = (String)date($key,$value);
        			break;
        			default:
        				$val[] = (Int)date($key,$value);
        			break;
        		}
        	} 
        } else if ( empty($value) ) {
        	$val = self::$defaults;
        }
       
        $value = (Array) $val;
        
        // check if element may have multiple values
        $multiple = '';

        if (substr($name, -2) == '[]') {
            // multiple implied by the name
            $multiple = ' multiple="multiple"';
        }

        if (isset($attribs['multiple'])) {
            // Attribute set
            if ($attribs['multiple']) {
                // True attribute; set multiple attribute
                $multiple = ' multiple="multiple"';

                // Make sure name indicates multiple values are allowed
                if (!empty($multiple) && (substr($name, -2) != '[]')) {
                    $name .= '[]';
                }
            } else {
                // False attribute; ensure attribute not set
                $multiple = '';
            }
            unset($attribs['multiple']);
        } 

        // now start building the XHTML.
        $disabled = '';
        if (true === $disable) {
            $disabled = ' disabled="disabled"';
        }

        // Build the surrounding select element first.
        $xhtml = '<select'
                . ' name="' . $this->view->escape($name) . '[]"'
                . ' id="' . $this->view->escape($id) . '"'
                . $multiple
                . $disabled
                . $this->_htmlAttribs($attribs)
                . ">\n    ";

        // build the list of options

		// month
		
        $array = array(
        	'month'=>'M',
        	'day'=>'j',
        	'year'=>'Y'
        );
        
        $string = '';
        $num = 0;
        foreach ( $array as $key=>$val ) {
        	
        	$defString = '';
        	$stringmax = strlen((String)self::$minMax[$val][1]);
        	while( strlen($defString) < $stringmax ) {
        		$defString .= $val;
        	}
        	
        	if (!isset($$key)) { $$key = array(); }
        	
        	$items = array();
        	
        	for ($i = self::$minMax[$val][0]; $i < self::$minMax[$val][1]; $i++ ) {
        		
        		if ( $key == 'month' ) {
        			$m = date("M", mktime(0,0,0,$i));
        			$items[] = $this->_build($m,$m,$value[$num]);
        		} else {
        			$items[] = $this->_build($i,$i,$value[$num]);
        		}
        	}
        	
        	$string .= $xhtml.implode("\n    ", $items) . "\n</select>";
        	
        	$num++;
        	
        }
        
        return $string;
		
    }

    protected function _build($value, $label, $selected = false)
    {

        $opt = '<option'
             . ' value="' . $this->view->escape($value) . '"'
             . ' label="' . $this->view->escape($label) . '"';
             
		if($value === $selected) { $opt .= ' selected="selected"';}
		
        $opt .= '>' . $this->view->escape($label) . "</option>";

        return $opt;
    }
}